//  Inside Microsoft Exchange 2007 Web Services
//  Chapter 8
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ProxyHelpers.EWS;

namespace CreateCalendarItemInNepalStandardTime
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            CreateCalendarItemInNepalStandardTime(binding);
        }

        /// <summary>
        /// Create a calendar item in Nepal Standard Time (Listing 8-9)
        /// </summary>
        /// <param name="binding">Binding to use for call</param>
        /// 
        public static void CreateCalendarItemInNepalStandardTime(
                                      ExchangeServiceBinding binding)
        {
            // Create a new Calendar Item for an appointment to occur on Februrary 26th,
            // 2007 at 8am in Kathmandu, Nepal using DateTime instances with 
            // DateTimeKind values of Unspecified, along with a MeetingTimeZone
            //
            CalendarItemType newCalendarItem = new CalendarItemType();
            // Create the DateTime instance as Unspecified
            //
            DateTime startTime = new DateTime(2007, 02, 26, 8, 00, 00,
                           DateTimeKind.Unspecified);

            newCalendarItem.Start = startTime;
            newCalendarItem.End = startTime.AddHours(1);
            newCalendarItem.StartSpecified = newCalendarItem.EndSpecified = true;

            // Create the MeetingTimeZone with a Time Zone bias of -PT5H45M
            //
            TimeSpan timeZoneBias = (new TimeSpan(0)).Subtract(new TimeSpan(5, 45, 0));
            newCalendarItem.MeetingTimeZone = new TimeZoneType();
            newCalendarItem.MeetingTimeZone.BaseOffset =
                           DateTimeHelpers.TimeSpanToXSDuration(timeZoneBias);
            newCalendarItem.Subject = "Feb 26th, 2007 8am meeting in Kathmandu using" +
                  "Unspecified DateTime instances (e.g. un-timezoned strings" +
                  "w/MeetingTimeZone defining the bias)";

            // Format the request and send
            //
            CreateItemType createItemRequest = new CreateItemType();
            createItemRequest.SendMeetingInvitations =
                         CalendarItemCreateOrDeleteOperationType.SendToNone;
            createItemRequest.SendMeetingInvitationsSpecified = true;
            createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
            createItemRequest.Items.Items = new CalendarItemType[1];
            createItemRequest.Items.Items[0] = newCalendarItem;

            // In real code, we would get a response object here and check for errors
            // accordingly.
            //
            CreateItemResponseType response = binding.CreateItem(createItemRequest);
            ItemInfoResponseMessageType responseMessage = response.ResponseMessages.Items[0] as
                      ItemInfoResponseMessageType;
            if (responseMessage.ResponseCode != ResponseCodeType.NoError)
            {
                throw new Exception("CreateItem failed with response code " +
                          responseMessage.ResponseCode.ToString());
            }
        }

    }
}
